package cool.taomu.box.jsch

import com.jcraft.jsch.JSch
import cool.taomu.box.inter.IDataSource
import java.util.Hashtable
import java.util.Properties
import org.eclipse.xtend.lib.annotations.Accessors
import org.slf4j.LoggerFactory

@Accessors
class JschDataSource implements IDataSource {
	val static LOG = LoggerFactory.getLogger(JschDataSource);
	String host;
	String uname;
	String passwd;
	int port;
	int timeout = 60000000;
	Hashtable<String, String> sshconfig;

	override resource() {
		try {
			var jsch = new JSch();
			var session = jsch.getSession(uname, host, port);
			session.password = passwd;
			if (sshconfig !== null) {
				session.setConfig(sshconfig);
			}
			// id_rsa jsch.addIdentity("","");
			var config = new Properties();
			config.put("StrictHostKeyChecking", "no");
			session.config = config;
			session.timeout = this.timeout;
			session.connect
			if (session.isConnected) {
				LOG.info("链接成功")
			} else {
				LOG.info("链接失败")
			}
			return session;
		} catch (Exception ex) {
			LOG.info("失败", ex)
		}
		return null;
	}
}
